package com.ctbu.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctbu.entity.LoanProduct;
import com.ctbu.entity.UserInfo;
import com.ctbu.service.UserInfoService;
import com.ctbu.tools.HttpStatusEnum;
import com.ctbu.tools.ResponseResult;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author lli
 * @since 2024年06月25日
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;

    /**
     * 分页查询 含快捷查询
     * @param pageNum 当前页默认1
     * @param pageSize  每页大小默认5
     * @return
     */
    @GetMapping
    public ResponseResult<Object> list(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize,
            @RequestParam(value = "fullName",required = false,defaultValue = "") String fullName,
            @RequestParam(value = "username",required = false,defaultValue = "") String username,
            @RequestParam(value = "userType",required = false,defaultValue = "") String userType,
            @RequestParam(value = "start",required = false,defaultValue = "") String start,
            @RequestParam(value = "end",required = false,defaultValue = "") String end
    ){
        return userInfoService.selectList(pageNum,pageSize,fullName,username,userType,start,end);

    }

    /**
     * post:增加  put:修改 delete:删除 get:查询
     */

    @GetMapping("/{id}")
    public ResponseResult<UserInfo> queryLoanProductById(@PathVariable String id){
        UserInfo userInfo=userInfoService.getById(id);
        return ResponseResult.success(userInfo);
    }

    /**
     * @param userInfo
     * @return
     * @RequestBody 接收前端传递的JSON数据的类型
     */
    @PostMapping
    public ResponseResult<Boolean> save(@RequestBody UserInfo userInfo){
        boolean b=userInfoService.save(userInfo);
        if(b){
            return ResponseResult.success(b,"增加用户成功");
        }
        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"增加用户失败");
    }

    @PutMapping
    public ResponseResult<Boolean> update(@RequestBody UserInfo userInfo){
        boolean b=userInfoService.updateById(userInfo);
        if(b){
            return ResponseResult.success(b,"修改用户成功");
        }
        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"修改用户失败");
    }

    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable String id){
        boolean b=userInfoService.removeById(id);
        if(b){
            return ResponseResult.success(b,"删除用户成功");
        }
        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"删除用户失败");
    }

    /**
     * 登录
     */
    @PostMapping("/checkLogin")
    public ResponseResult<Object> checkLogin(HttpServletRequest request,@RequestBody UserInfo userInfo){
        //读取存储的验证码
        ServletContext context=request.getServletContext();
        String backCode=(String) context.getAttribute("backCode");
        //先验证前端传过来的验证码是否和保存的一致
        if(!backCode.equalsIgnoreCase(userInfo.getVerifyCode())){
            return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"验证码错误");
        }
        //验证账号密码信息
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUsername,userInfo.getUsername());
        wrapper.eq(UserInfo::getPassword,userInfo.getPassword());
        UserInfo user=userInfoService.getOne(wrapper);
        if(user==null){
            return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"账号或密码错误");
        }
        //脱敏
        user.setPassword("******");
        return ResponseResult.success(user,"登陆成功");
    }

    @PostMapping("/register")
    public ResponseResult<Object> register(@RequestBody UserInfo userInfo){
        userInfo.setAvatar("");
        userInfo.setFullName("");
        userInfo.setEmail("");
        userInfo.setAddress("");
        userInfo.setGender("2");

        boolean result = userInfoService.save(userInfo);
        if (result){
            return ResponseResult.success(result,"注册成功");
        }
        return ResponseResult.success(result,"注册失败!请联系管理人员");
    }

    /**
     * 更新密码
     * @param userInfo
     * @return
     */
    @PutMapping("/updatePassword")
    public ResponseResult<Object> updatePassword(@RequestBody UserInfo userInfo){
        //1.通过传输对象中的id查询完整的数据
        UserInfo user = userInfoService.getById(userInfo.getUserId());
        //2.判断传输的原始密码于查询密码是否一致
        if (!user.getPassword().equals(userInfo.getPassword())){
            return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"原始密码错误");
        }
        //3.修改新密码
        userInfo.setPassword(userInfo.getPasswordNew());
        boolean result = userInfoService.updateById(userInfo);
        if (result){
            return ResponseResult.success(result,"修改密码成功");
        }

        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"修改密码失败,请联系管理人员");

    }

}
